package com.fearless.crm.workbench.service.impl;

import com.fearless.crm.settings.dao.UserDao;
import com.fearless.crm.settings.entity.User;
import com.fearless.crm.utils.UUIDUtil;
import com.fearless.crm.vo.CustomerPaginationVo;
import com.fearless.crm.workbench.dao.ContactsDao;
import com.fearless.crm.workbench.dao.CustomerDao;
import com.fearless.crm.workbench.entity.Contacts;
import com.fearless.crm.workbench.entity.Customer;
import com.fearless.crm.workbench.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class CustomerServiceImpl implements CustomerService {


    @Autowired
    private CustomerDao customerDao;

    @Resource
    private ContactsDao contactsDao;

    @Resource
    private UserDao userDao;


//自动补全,业务层方法
    public List<String> getCustomerNameAuto(String name) {


        List<String> customerList=customerDao.getCustomerByNameAutoDao(name);

        return customerList;
    }



//展现客户列表,业务层方法
    public CustomerPaginationVo getCustomerListService(Map map1) {

        int total=customerDao.getCustomerTotalDao();

        List<Customer> customerList=customerDao.getCustomerListDao(map1);


        CustomerPaginationVo customerPaginationVo=new CustomerPaginationVo();
        customerPaginationVo.setDataList(customerList);
        customerPaginationVo.setTotal(total);

        return customerPaginationVo;
    }



//执行新增客户操作
    public boolean saveCustomerService(Customer customer) {

        boolean flag=true;
        int count=customerDao.saveCustomerDao(customer);
        if(count!=1){
            flag=false;
        }

        return flag;
    }



//铺垫修改客户的模态窗口
    public Customer getCustomerByIdService(String customerId)   {
               Customer customer= customerDao.getCustomerById(customerId);
        return customer;
    }



//执行修改客户的操作
    public boolean updateCustomer(Customer customer) {
        boolean flag=true;
        int count=customerDao.updateCustomer(customer);

        if(count!=1){
            flag=false;
        }

        return flag;
    }



//执行删除客户的操作
    public boolean deleteCustomer(String[] id) {
        boolean flag=true;
       /* for (String s:id){
           int count= customerDao.deleteCustomer(s);

        }*/
        int count=customerDao.deleteCustomer(id);
        if(count==0){
            flag=false;
        }


        return flag ;
    }

//跳转到客户详细页,业务层方法
    public Customer getCustomerByIdDetail(String id) {
        Customer customer=customerDao.getCustomerByIdDetail(id);

        return customer;
    }


//在客户详细页新增联系人
    public boolean addContactInCustomerDetail(Contacts contacts, String customerName) {
        String id=UUIDUtil.getUUID();
        boolean judgement=true;

            //判断用户填写的客户是否存在，如果不存在，则创建一个
        Customer customer=customerDao.queryCustomer(customerName);
        if(customer==null){
            customer=new Customer();
            customer.setId(id);
            customer.setCreateTime(contacts.getCreateTime());
            customer.setCreateBy(contacts.getCreateBy());
            customer.setOwner(contacts.getOwner());
            customer.setName(customerName);

            int count =customerDao.saveCustomerInAddContact(customer);
            if(count!=1){
                judgement=false;

            }
        }

        //程序走到这里customer就不可能为空了
        //如果存在，就将客户的id注入到contact中
        contacts.setCustomerId(customer.getId());

        System.out.println("在service中的contact"+contacts.toString());

        int count=contactsDao.addContactInCustomerDetail(contacts);

        if(count!=1){
            judgement=false;
        }



        return judgement;
    }

//铺垫客户详细页修改客户的模态窗口
    public Map editCustomerInDetail(String customerId) {
        List<User> userList=userDao.getUserListDao();
        Customer customer = customerDao.getCustomerById(customerId);
        Map map=new HashMap();
        map.put("userList",userList);
        map.put("customer",customer);

        return map;
    }



//在客户详细页执行修改客户的操作
    public Map updateCustomerInDetail(Customer customer) {
        boolean flag=true;

        String name=customer.getOwner();
        System.out.println(name);
        User user=userDao.getUser(name);

        customer.setOwner(user.getId());

        int count=customerDao.updateCustomerInDetail(customer);
        if(count!=1){
            flag=false;
        }
        Map map=new HashMap();
        map.put("success",flag);

        return map;
    }

//执行在客户详细页删除客户的操作

    public boolean deleteCustomerInDetail(String id) {
        boolean flag=true;

        int count=customerDao.deleteCustomerInDetail(id);

        if(count!=1){
            flag=false;
        }


        return flag;
    }
}
